<?php

use app\common\service\ConfigService;
use DI\Container;
use think\facade\Db;

/**
 * Here is your custom functions.
 */
if (!function_exists('env')) {
    function env(string $key = null, $default = null)
    {
        $loader = new WebmanEnv\EnvLoader(base_path('.env'));
        return $loader::get($key, $default);
    }
}

if (!function_exists('di')) {
    function di(): Container
    {
        global $container;
        if (!$container) {
            $container = new Container();
        }
        return $container;
    }
}

if (!function_exists('plugin_url')) {
    function plugin_url($path, $data = [])
    {
        return "/app/" . $path;
    }
}
if (!function_exists('img_url')) {
    function img_url($path)
    {
        if (str_starts_with($path, "http://") || str_starts_with($path, "https://") || str_starts_with($path, "//")) {
            return $path;
        }
        $config = ConfigService::getConfig("FILE_UPLOAD_CONFIG");
        $domain = $config['domain'] ?? "";

        if (str_starts_with($path, "{DOMAIN}")) {
            return str_replace("{DOMAIN}", $domain, $path);
        }
        return $domain . $path;
    }
}

if (!function_exists('wrapperStatus')) {

    function wrapperStatus($arr)
    {
        $new = [
            [
                'label' => "全部",
                'value' => "",
            ]
        ];
        foreach ($arr as $key => $value) {
            $new[] = [
                'label' => $value,
                'value' => $key,
            ];
        }
        return $new;
    }

}

if (!function_exists('getLastSql')) {
    function getLastSql()
    {
        return Db::getlastsql();
    }
}

if (!function_exists('strToArr')) {
    function strToArr($string, $separator = ",")
    {
        if (is_array($string)) {
            return $string;
        }
        return $string ? array_values(array_filter(explode($separator, $string))) : [];
    }
}